<?php
/**
 * Necros Model for Congregation Component
 * 
 * @package    Congregation
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4
 * @license		GNU/GPL
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.model');

/**
 * Sharing Sharing Model
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */
class CongregationModelNecros extends CongregationModel
{

	var $_data;


  /**
   * Returns the query for listing the categories
   *
   * @access  protected
   * @return  string    The query to be used to retrieve the categories data from the database
   * @since   1.5.5
   */
  function _buildQuery()
  {
	
	
			$query = " SELECT n.*, m.fname, m.lname, m.ddate, m.dplace, s.abbr FROM #__congregation_necro n 
			LEFT JOIN #__congregation_profile m ON n.username=m.username
			LEFT JOIN #__congregation_status AS s ON m.status=s.id 
			". $this->_buildWhere() ."
			". $this->_buildOrderby();
	

    return $query;
  }


	function _buildWhere()
	{
	
		$search = JRequest::getVar('search');
		$year	= JRequest::getVar('year'); 
	
		$where = array();
		
		if($year)
		{
			$where[] = " `ddate` >= '" . $year . "-01-01' AND `ddate` <= '" . $year . "-12-31' ";
		}
		if ($search) {
			$where[] = '(LOWER(`fname`) LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false ) . ' OR LOWER(`lname`) LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false ) . ')';
		}
		
		$where = count($where) ? 'WHERE ' . implode(' AND ', $where) : '';

		return $where;
	}
	
	function _buildOrderby()
	{
		$order_by = JRequest::getVar('order_by');
		$order_dir = JRequest::getVar('order_dir');

		$ordering	= ' ORDER BY '. $order_by .' '. $order_dir;

		return $ordering;

	}
  
	/**
	* Method to get necro list
	*
	*/

	
	function getData()
	{
		// Lets load the data if it doesn't already exist

		if (empty( $this->_data ))
		{
			
			$query = $this->_buildQuery();
			$limit      = JRequest::getVar('limit', 0, '', 'int');
			$limitstart = JRequest::getVar('limitstart', 0, '', 'int');
			$query .= " LIMIT $limitstart, $limit";
			
			$this->_db->setQuery( $query );
			
			$this->_data = $this->_getList( $query );
			
		}

		return $this->_data;
	
	}
	
		/**
		* Method to get the total number of necros
		*
		* @access  public
		* @return  int     The total number of categories
		* @since   1.5.5
		*/
		function getTotal()
		{
		// Let's load the number of categories if it doesn't already exist
		if (empty($this->_total))
		{
			$query = $this->_buildQuery();
			
			$this->_total = $this->_getListCount($query);
			
		}

		return $this->_total;
		}
		
		function getYears()
		{
			$query = "SELECT LEFT(`ddate`, 4) as `year` FROM #__congregation_profile WHERE ddate <> '0000-00-00' GROUP BY  `year` ORDER BY `year`";
			$this->_db->setQuery( $query );
			return $this->_getList( $query );
			
		}

}